package com.nathan.crm.controller;

import com.nathan.base.BaseController;
import com.nathan.base.ResultInfo;
import com.nathan.crm.query.CustomerQuery;
import com.nathan.crm.service.CustomerService;
import com.nathan.crm.vo.Customer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Map;

@Controller
@RequestMapping("customer")
public class CustomerController extends BaseController {

    @Autowired
    private CustomerService customerService;

    /**
     * 多条件查询客户信息
     *
     * @param customerQuery
     * @return
     */
    @GetMapping("list")
    @ResponseBody
    public Map<String,Object> queryCustomerByParams(CustomerQuery customerQuery){
        return customerService.queryByParamsForTable(customerQuery);
    }

    /**
     * 跳转到客户信息管理页面
     *
     * @return
     */
    @RequestMapping("index")
    public String index(){
        return "customer/customer";
    }

    /**
     * 打开客户添加或更新页面
     *
     * @return
     */
    @RequestMapping("addOrUpdateCustomerPage")
    public String addOrUpdateCustomerPage(Integer id, Model model){
        // 更新操作
        if (id != null){
            Customer customer = customerService.selectByPrimaryKey(id);
            model.addAttribute("customer",customer);
        }

        return "customer/add_update";
    }

    /**
     * 添加客户
     * @param customer
     * @return
     */
    @PostMapping("add")
    @ResponseBody
    public ResultInfo addCustomer(Customer customer){
        customerService.addCustomer(customer);

        return success("添加客户成功！");
    }

    /**
     * 修改客户信息
     * @param customer
     * @return
     */
    @PostMapping("update")
    @ResponseBody
    public ResultInfo updateCustomer(Customer customer){
        customerService.updateCustomer(customer);

        return success("客户信息修改成功！");
    }

    /**
     * 删除客户
     * @param id
     * @return
     */
    @PostMapping("delete")
    @ResponseBody
    public ResultInfo deleteCustomer(Integer id){
        customerService.deleteCustomer(id);
        return success("删除客户成功");
    }

    /**
     * 打开订单页面
     * @param cid
     * @return
     */
    @RequestMapping("orderInfoPage")
    public String orderInfoPage(Integer cid,Model model){
        // 根据客户id查询客户信息,存入到请求域中
        model.addAttribute("customer",customerService.selectByPrimaryKey(cid));

        return "customer/customer_order";
    }

}
